package com.ddwl.user.service;

import com.ddwl.common.exception.ParamException;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.AccountCreateBo;
import com.ddwl.schema.bo.user.AccountQueryBo;
import org.springframework.util.CollectionUtils;
import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ExceptionCode;
import com.ddwl.common.util.*;
import com.ddwl.schema.bo.user.AccountBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.user.AccountVo;
import com.ddwl.user.dao.AccountDao;
import com.ddwl.user.dao.model.Account;
import com.ddwl.user.dao.model.User;
import com.ddwl.user.dao.UserRoleDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/12 18:29
 */
@Service
public class AccountService {

    @Resource
    private AccountDao accountDao;
    @Resource
    private UserRoleDao userRoleDao;

    /**
     * 检查商户登录帐号是否存在
     *
     * @param username
     * @return
     */
    public Boolean check(String username) {
        Account entity = accountDao.queryAccountByUserName(username);
        return entity != null;
    }

    public AccountVo getById(String uid) {
        Account entity = accountDao.selectByPrimaryKey(uid);
        ParamException.isNull(entity,"账户信息不存在");
        AccountVo vo = new AccountVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Transactional(rollbackFor = Throwable.class)
    public String create(AccountCreateBo bo) {
        Account entity = accountDao.queryAccountByUserName(bo.getUsername());
        if (entity != null) {
            throw new CommonException(ExceptionCode.USER_EXISTS);
        }
        entity = BeanConverUtils.convertBean(bo,Account.class);
        String salt = RandomGenerateUtil.getRandomS(0, 6);
        entity.setId(UUIDGenerator.getShortUUID());
        entity.setSalt(salt);
        entity.setPassword(EncryptUtil.encryptMD5(salt + bo.getPassword()));
        entity.setPasswordStatus(1);
        accountDao.insert(entity);
        return entity.getId();
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(AccountBo bo) {
        Account entity = accountDao.selectByPrimaryKey(bo.getId());
        if (entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        BeanUtils.copyProperties(bo, entity, ObjectUtil.getNullFields(bo));
        if(StringUtils.hasText(bo.getPassword())) {
            entity.setPassword(EncryptUtil.encryptMD5(entity.getSalt() + bo.getPassword()));
        }
        accountDao.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void forbid(AccountBo bo) {
        Account entity = accountDao.selectByPrimaryKey(bo.getId());
        if (entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        entity.setStatus(bo.getStatus());
        accountDao.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id) {
        accountDao.deleteById(id);
    }



    public PageVo<AccountVo> page(PageBo<AccountQueryBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(),pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(),pageBo.isAsc()));
        PageInfo<User> info = new PageInfo(accountDao.queryPage(pageBo.getParam()));
        List<AccountVo> list = BeanConverUtils.convertBeanList(info.getList(),AccountVo.class);
        return new PageVo<>(info.getTotal(),pageBo.getPage(),pageBo.getPageSize(),list);
    }

    public void changeId() {
        List<Account> list=accountDao.getAll();
        if (!CollectionUtils.isEmpty(list)) {
            Map<String, String> map = new HashMap<>();
            for (Account account : list) {
                String id = account.getId();
                if (id.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(id);
                    map.put(id, String.valueOf(longId));
                }
            }
            if (!CollectionUtils.isEmpty(map)) {
                accountDao.changeId(map);
                userRoleDao.changeUserId(map);
            }
        }
    }

}
